<?php

namespace App\Repositories;

use App\Models\UserTag;
use Cache;
class UserTagRepository extends Repository
{
    static $tag = 'testing.user.tag';


    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.cache_key.testing_user_tag.info') . $id;
        $value = $this->remember($cacheKey, function () use ($id) {
            return UserTag::where('id', $id)->first();
        });
        return $value;
    }


    public function getLatestUserTag($uid,$tid){
        $cacheKey = getCacheKey('testing.cache_key.testing_user_tag.latest_tag_info') . $tid.'_'.$uid;
        $datas = Cache::tags($this->tag())->remember($cacheKey, 60*24,function () use ($uid,$tid) {
            $data = UserTag::where('uid', $uid)->where('tid',$tid)->orderBy('created_at','desc')->first();
            return $data;
        });
        return $datas;
    }

     public function store($data)
     {
        $this->clearCache($this->tag());
//        $res = UserTag::firstOrCreate($data);
        $res = UserTag::create($data);
        return $res;
     }


    /**联合user_record表查询
     * @return mixed
     */
    public function getUserTagByTid($tid,$start,$end)
    {
        return UserTag::select('uid','tag','created_at')
            ->where('tid',$tid)
            ->where('created_at','<',$end)
            ->where('created_at','>',$start)
            ->get()
            ->toarray();
    }



    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(UserTag::class);
    }

    public function tag()
    {
        return self::$tag;
    }
}